package com.wyg.epdata.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyg.epdata.entity.DataResult;
import com.wyg.epdata.entity.es.DataResultES;
import com.wyg.epdata.entity.reqDto.EntityReq;
import com.wyg.epdata.entity.reqDto.KwPageReq;
import com.wyg.epdata.service.ArticleEntityService;
import com.wyg.epdata.service.CompanyNumService;
import com.wyg.epdata.service.DataResultService;
import com.wyg.epmysql.common.page.PageReq;
import com.wyg.epmysql.common.page.PageRes;
import com.wyg.epcommonbase.common.ApiResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: WangYongGan
 * @Date: 2022/3/5 23:37
 * @Description:
 */
@Api(tags = "查询文章")
@RestController
@RequestMapping("article")
public class ArticleController {
    public static String index = "es_index";
    @Autowired
    DataResultService dataResultService;
    @Autowired
    CompanyNumService companyNumService;
    @Autowired
    ArticleEntityService articleEntityService;
    @Autowired
    ElasticsearchClient elasticsearchClient;

    @ApiOperation("根据企业名称--分页")
    @PostMapping("byEntity")
    public ApiResult byEntity(@RequestBody PageReq<EntityReq, DataResult> req) {
        final Page<DataResult> page = req.toPage();
        Integer x = (Integer) req.getParam().getParam().get("reg");
        QueryWrapper<DataResult> wrapper = new QueryWrapper<>();
        String source = (String) req.getParam().getParam().get("source");
        if (!StringUtils.isEmpty(source))
            wrapper.lambda().eq(DataResult::getSource, source);
        final List<String> idsByEntity = articleEntityService.getIdsByEntity(req.getParam().getEntity(), x == null ? 2 : x);
        dataResultService.page(page, wrapper.lambda().in(DataResult::getArticleId, idsByEntity));
        PageRes<DataResult> pageRes = new PageRes<>();
        pageRes.setCurrent(page.getCurrent());
        pageRes.setSize(page.getSize());
        pageRes.setTotal(page.getTotal());
        try {
            final List<Date> collect = pageRes.getRecords().stream().map(o -> ((DataResult) o).getCreatedAt()).collect(Collectors.toList());
        } catch (Exception ignored) {

        }
        final List<DataResult> records = page.getRecords();
        pageRes.setRecords(records);
        return ApiResult.ok().data(pageRes);
    }

    @SneakyThrows
    @ApiOperation("根据关键字查询文章")
    @PostMapping("getByKW")
    public ApiResult getByKW(@RequestBody KwPageReq<String> kwPageReq) {
        final String keyword = kwPageReq.getParam();
        SearchResponse<DataResultES> response = elasticsearchClient.search(s -> s.index(index).query(_1 -> _1.match(_2 -> _2.field("raw_text").query(keyword))).size(kwPageReq.getSize()).from(kwPageReq.getCurrent()), DataResultES.class);
        final List<Hit<DataResultES>> hits = response.hits().hits();
        return ApiResult.ok().data(hits.stream().map(Hit::source));
    }

}
